/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.CashBalanceDAO;
import com.ivanbiz.dao.impl.CashBalanceDAOImpl;
import com.ivanbiz.model.CashBalance;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class SaldoKasService extends SelectorComposer<Component> {

    @Wire
    Listbox listSaldoKas;
    CashBalanceDAO cashBalanceDAO;
    NumberFormat numberFormat;
    SimpleDateFormat sdf;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        cashBalanceDAO = new CashBalanceDAOImpl();
        numberFormat = NumberFormat.getNumberInstance();
        sdf = new SimpleDateFormat("dd MMM yyyy");
        loadSaldoKas();
    }

    public void loadSaldoKas() {
        try {
            listSaldoKas.getItems().clear();
            int no = 0;
            int x = 0;
            List<CashBalance> loadSaldoKas = cashBalanceDAO.getAll(CashBalance.class);
            for (CashBalance cashBalance : loadSaldoKas) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(cashBalance);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(sdf.format(cashBalance.getDateBalance())));
                item.appendChild(new Listcell(cashBalance.getGlAccount().getNameGL() + " A/C No. " + cashBalance.getGlAccount().getNoGL()));
                item.appendChild(new Listcell("Rp " + numberFormat.format(cashBalance.getBalance()) + ",00"));
                listSaldoKas.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(PengajarService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Window win = (Window) Executions.createComponents("/saldoKasAction.zul", null, null);
        win.doModal();
        loadSaldoKas();
    }

    @Listen("onClick=#hapus;")
    public void hapus() throws Exception {
        if (listSaldoKas.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di hapus");
        } else {
            CashBalance cashBalance = listSaldoKas.getSelectedItem().getValue();
            cashBalanceDAO.delete(cashBalance);
            loadSaldoKas();
        }
    }
}
